<?php
namespace app\admin\service;

use think\Model;
use think\Session;
use think\Db;
class Order extends Model
{

    protected $name='love_customer';



    /**
     * 获取录客总数 (可指定某天)
     */
    public function getPayOrderTotal($startDate = null, $endDate = null){
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['create_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate) + 86400],
            ];
        }
        return $this->getOrderTotal($filter);
    }


    /**
     * 获取客户总数量
     * @param array $filter
     * @return int|string
     * @throws \think\Exception
     */
    private function getOrderTotal($filter = []){
        $sessionInfo=Session::get('yoshop_admin');
        $loginuserInfo=Db::name('love_admin_user')->where(['id'=>$sessionInfo['user']['id']])->find();
        if($loginuserInfo['type']==1){
            //主管理员
            $where['floweuserid']=['>',0];
        }elseif ($loginuserInfo['type']==2){
            //区域管理员
            //获取所有区域销售人员id
            $user=Db::name('love_admin_user')->where(['cityid'=>$loginuserInfo['cityid'],'type'=>['in','4,5']])->field('id,type,cityid')->select()->toArray();
            if($user){
                foreach ($user as $k=>$v){
                    $ids[]=$v['id'];
                }
            }
            $ids[]=$loginuserInfo['id'];
            $where['floweuserid']=['in',$ids];
        }else{
            $user=Db::name('love_admin_user')->where(['areaid'=>$loginuserInfo['areaid'],'type'=>['in','4,5']])->field('id,type,cityid')->select()->toArray();
            if($user){
                foreach ($user as $k=>$v){
                    $ids[]=$v['id'];
                }
            }
            $ids[]=$loginuserInfo['id'];
            $where['floweuserid']=['in',$ids];
        }
        $new=array_merge($where,$filter);
        return db('love_customer')->where($new)->count();
    }


}